##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  figure_xi.R 
#  -- file produces the density plots Figure xi in appendix
#
##########################################################

rm(list=ls())
source('~/Dropbox/Seeing_Spots/replication/pre_data_vagov.R')

ymat=cbind(video_skipped, replay, share, getlink, all_y,time_watched, total_time)

newsint_old = newsint - 2 
#newsint = (rowSums(cbind(sen_majority_correct,hou_majority_correct,(newsint+1))))-2    
newsint_alt = rowSums(cbind(sen_majority_correct,hou_majority_correct))-1 #,(newsint+1))))-2    
#newsint[which(newsint<0)] = -1
#newsint[which(newsint>0)] =  1

college=as.numeric(va_data$educ)
college[which(as.numeric(va_data$educ)<3)]=-1
college[which(as.numeric(va_data$educ)==3 | as.numeric(va_data$educ)==4)]=0
college[which(as.numeric(va_data$educ)>4)]=1

newsint_edu = college

# density plots - interesteds

completes = !is.na(time_watched) & !is.na(pid_lean) & !is.na(newsint_old)

ytime 			= time_watched[completes] 
tr_pos_ad 		= pos_ad[completes] 
tr_neg_ad    	= neg_ad[completes] 
tr_dem_ad   	= dem_ad[completes] 
tr_rep_ad  	= rep_ad[completes] 
tr_dem_pos	= pos_ad[completes] 
tr_dem_neg  	= neg_ad[completes] 
tr_rep_pos 	= dem_ad[completes] 
tr_rep_neg 	= rep_ad[completes] 
pid				= pid_lean[completes] 
newsint_alt		= newsint_alt[completes] 
newsint_old		= newsint_old[completes] 
newsint_edu		= newsint_edu[completes] 

#lows = newsint_old==-1
#meds = newsint_old== 0
#high = newsint_old== 1       

lows = newsint_alt==-1
meds = newsint_alt== 0
high = newsint_alt== 1

                     
dems = pid==-1
inds = pid== 0
reps = pid== 1    



{    
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/density_cand_by_dem_pid_both_VA.pdf')         
# dem v. rep; by dem pid
	plot(density(c(ytime[dems & tr_rep_ad]),from=0,to=100),main='',xlab='Time Watched',ylim=c(0,.09),col='black',lwd=2)  
	lines(density(ytime[c(dems & tr_dem_ad)],from=0,to=100),main='',xlab='Time Watched',lty=2,col='black',lwd=2)      
	legend(cex=1.45,x=c(1),y=c(.075),legend=c('McAuliffe Ad'),lty=c(2),box.col="#0000ff00",col='black',lwd=2)
	legend(cex=1.45,x=c(1),y=c(.08),legend=c('Cuccinelli Ad'),lty=c(1),box.col="#0000ff00",col='black',lwd=2)
	#text(cex=1.45,x=c(90),y=c(.075),label=c(paste('p = ',round(digits=3,ks.test(c(ytime[dems & tr_rep_ad]),ytime[c(dems & tr_dem_ad)])$p.value))),
	#  col='black',lwd=2)
	ksp=round(ks.test(c(ytime[dems & tr_rep_ad]),ytime[c(dems & tr_dem_ad)])$p.value,digits=3)
	tp=round(t.test(c(ytime[dems & tr_rep_ad]),ytime[c(dems & tr_dem_ad)])$p.value,digits=3)  

	text(cex=1.45,x=c(86),y=c(.085),label=c(expression(italic(paste(p,' = ',0.081),sep=''))),
	  col='black',lwd=2)
#	text(cex=1.45,x=c(85),y=c(.078),label=c(expression(italic(paste(p[ks-test],' = ',0.095),sep=''))),
#	  col='black',lwd=2)
	
dev.off()	
}



{         
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/density_cand_by_ind_pid_both_VA.pdf')         
# dem v. rep; by ind pid
	plot(density(c(ytime[inds & tr_rep_ad]),from=0,to=100),main='',xlab='Time Watched',ylim=c(0,.09),col='black',lwd=2)    
	lines(density(ytime[c(inds & tr_dem_ad)],from=0,to=100),main='',xlab='Time Watched',lty=2,col='black',lwd=2)            
	legend(cex=1.45,x=c(1),y=c(.08),legend=c('Cuccinelli Ad'),lty=c(1),box.col="#0000ff00",col='black',lwd=2)   
	legend(cex=1.45,x=c(1),y=c(.075),legend=c('McAuliffe Ad'),lty=c(2),box.col="#0000ff00",col='black',lwd=2)  
	#text(cex=1.45,x=c(90),y=c(.075),label=c(paste('p = ',round(digits=3,ks.test(c(ytime[inds & tr_rep_ad]),ytime[c(inds & tr_dem_ad)])$p.value))),
	#  col='black',lwd=2)
	ksp=round(ks.test(c(ytime[inds & tr_rep_ad]),ytime[c(inds & tr_dem_ad)])$p.value,digits=3)
	tp=round(t.test(c(ytime[inds & tr_rep_ad]),ytime[c(inds & tr_dem_ad)])$p.value,digits=3)  

	text(cex=1.45,x=c(86),y=c(.085),label=c(expression(italic(paste(p,' = ',0.247),sep=''))),
	  col='black',lwd=2)
#	text(cex=1.45,x=c(85),y=c(.078),label=c(expression(italic(paste(p[ks-test],' = ',0.449),sep=''))),
#	  col='black',lwd=2)
dev.off()
}       



{  
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/density_cand_by_rep_pid_both_VA.pdf')          
# dem v. rep; by rep pid
	plot(density(c(ytime[reps & tr_rep_ad]),from=0,to=100),main='',xlab='Time Watched',ylim=c(0,.09),col='black',lwd=2)    
	lines(density(ytime[c(reps & tr_dem_ad)],from=0,to=100),main='',xlab='Time Watched',lty=2,col='black',lwd=2)            
	legend(cex=1.45,x=c(1),y=c(.08),legend=c('Cuccinelli Ad'),lty=c(1),box.col="#0000ff00",col='black',lwd=2)   
	legend(cex=1.45,x=c(1),y=c(.075),legend=c('McAuliffe Ad'),lty=c(2),box.col="#0000ff00",col='black',lwd=2)     
	#text(cex=1.45,x=c(90),y=c(.075),label=c(paste('p = ',round(digits=3,ks.test(c(ytime[reps & tr_rep_ad]),ytime[c(reps & tr_dem_ad)])$p.value))),
	#  col='black',lwd=2) 
	ksp=round(ks.test(c(ytime[reps & tr_rep_ad]),ytime[c(reps & tr_dem_ad)])$p.value,digits=3)
	tp=round(t.test(c(ytime[reps & tr_rep_ad]),ytime[c(reps & tr_dem_ad)])$p.value,digits=3)  

	text(cex=1.45,x=c(86),y=c(.085),label=c(expression(italic(paste(p,' = ',0.002),sep=''))),
	  col='black',lwd=2)
#	text(cex=1.45,x=c(85),y=c(.078),label=c(expression(italic(paste(p[ks-test],' = ',0.009),sep=''))),
#	  col='black',lwd=2)
dev.off()
}  

# END figure_xi.R